package com.shuoyue.ycgk.ui.course.info;

import android.content.Intent;
import android.os.Bundle;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.entity.TimerTable;
import com.shuoyue.ycgk.ui.course.adapter.TimerTableSimpleAdapter;
import com.shuoyue.ycgk.ui.course.playvideo.PlayVideoActivity;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;

public class FragmentCourseTimetable extends BaseCourseInfoFragment {
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.list)
    RecyclerView list;

    ArrayList<TimerTable> timetable;
    //    String thumb;
    TimerTableSimpleAdapter timerTableAdapter;
    //    int courseId;
    Course course;
    int isPay = 0;

    public static FragmentCourseTimetable newInstance(ArrayList<TimerTable> timetable,
                                                      Course course, int isPay) {
        FragmentCourseTimetable fragment = new FragmentCourseTimetable();
        Bundle args = new Bundle();
        args.putSerializable("timetable", timetable);
        args.putSerializable("course", course);
        args.putInt("isPay", isPay);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initParams(Bundle bundle) {
        timetable = (ArrayList<TimerTable>) bundle.getSerializable("timetable");
        course = (Course) bundle.getSerializable("course");
        isPay = bundle.getInt("isPay");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_course_timetable;
    }

    @Override
    protected void initView() {
        timerTableAdapter = new TimerTableSimpleAdapter(null,
                isPay != 1, timerTable -> {
            setAllFalseSelect(timerTableAdapter.getData());
            if (course.getIsApp() != 1) {
                toast("该课程不能在APP上播放");
                return;
            }
            startActivityForResult(new Intent(mContext, PlayVideoActivity.class)
                    .putExtra("table", timerTableAdapter.getData())
                    .putExtra("play", timerTable.getId())
                    .putExtra("isPay", isPay)
                    .putExtra("course", course), 1002);
        }, "章");
        list.setAdapter(timerTableAdapter);
        list.setLayoutManager(new LinearLayoutManager(mContext));
        list.addItemDecoration(new SpacesItemDecoration(1));
        scrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if (scrollY <= 0) {
                isScrollAtTop = true;
            } else {
                isScrollAtTop = false;
            }
        });
        registEmptyView(timerTableAdapter);

    }

    void setAllFalseSelect(ArrayList<TimerTable> tables) {
        for (TimerTable p : tables) {
            p.setPlaying(false);
            p.setSelect(false);
            if (p.getNodes() != null) {
                setAllFalseSelect(p.getNodes());
            }
        }
    }

    @Override
    protected void initData() {
        settimerTable(timetable);
    }

    void settimerTable(ArrayList<TimerTable> timetables) {
        timerTableAdapter.resetData(timetables);

    }

    @Override
    public String getTitle() {
        return "课程表";
    }

    public void resetTimeTable(ArrayList<TimerTable> timerTables) {
        timerTableAdapter.resetData(timerTables);
    }
}
